package com.useeinfo.oa.open.api;

import com.alibaba.fastjson.JSONObject;
import com.useeinfo.framework.sugar.data.QueryParam;
import com.useeinfo.framework.sugar.tools.StringConverters;
import com.useeinfo.oa.common.utils.ResultUtil.ResultDto;
import com.useeinfo.oa.common.utils.ResultUtil.ResultUtil;
import com.useeinfo.oa.modules.base.biz.ApplicationModuleBiz;
import com.useeinfo.oa.modules.base.entity.ApplicationModule;
import com.useeinfo.oa.modules.base.entity.Enterprise;
import com.useeinfo.oa.modules.base.entity.Position;
import com.useeinfo.oa.modules.inspection.biz.InspectionFocusBiz;
import com.useeinfo.oa.modules.inspection.biz.InspectionOrderBiz;
import com.useeinfo.oa.modules.maintaining.biz.MaintainFocusBiz;
import com.useeinfo.oa.modules.maintaining.biz.MaintainOrderBiz;
import com.useeinfo.oa.modules.repair.biz.RepairFocusBiz;
import com.useeinfo.oa.modules.repair.biz.RepairOrderBiz;
import com.useeinfo.oa.modules.security.biz.SystemUserBiz;
import com.useeinfo.oa.modules.security.entity.SystemUser;
import com.useeinfo.oa.modules.security.util.SessionKeyUtils;
import com.useeinfo.oa.modules.task.biz.TaskFocusBiz;
import com.useeinfo.oa.modules.task.biz.TaskOrderBiz;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @program: YiYouFuPlatform
 * @author: Taogq
 * @createDate: 2019-02-21
 * @createTime: 10:42:18
 */
@RestController
@RequestMapping("/api/getCountMsg")
public class CountMessageAPI {
    @Autowired
    private RepairOrderBiz repairOrderBiz;
    @Autowired
    private TaskOrderBiz taskOrderBiz;
    @Autowired
    private MaintainOrderBiz maintainOrderBiz;
    @Autowired
    private InspectionOrderBiz inspectionOrderBiz;
    @Autowired
    private ApplicationModuleBiz applicationModuleBiz;
    @Autowired
    private RepairFocusBiz repairFocusBiz;
    @Autowired
    private TaskFocusBiz taskFocusBiz;
    @Autowired
    private MaintainFocusBiz maintainFocusBiz;
    @Autowired
    private InspectionFocusBiz inspectionFocusBiz;
    @Autowired
    private SystemUserBiz systemUserBiz;

    /**
     * 获取用户拥有的模块
     *
     * @param request
     * @return
     */
    public List<ApplicationModule> getModel(HttpServletRequest request) {
        Enterprise enterprise = SessionKeyUtils.getSessionEnterprise(request);
        if (null == enterprise) {
            return new ArrayList<>();
        }
        String hql = "select obj from ApplicationModule obj where exists(select e from obj.enterpriseSet e where e.id=" + enterprise.getId() + ")";
        List<ApplicationModule> applicationModuleList = applicationModuleBiz.hqlExcute(hql);
        return applicationModuleList;
    }

    public boolean checkApplication(List<ApplicationModule> applicationModuleList, String remark) {
        for (ApplicationModule applicationModule : applicationModuleList) {
            if (StringConverters.null2String(applicationModule.getRemark()).equals(remark)) {
                return true;
            }
        }
        return false;
    }

    @RequestMapping("/getCount")
    public ResultDto getCount(HttpServletRequest request) {
        Enterprise enterprise = SessionKeyUtils.getSessionEnterprise(request);
        SystemUser systemUser = SessionKeyUtils.getSessionUser(request);
        List<ApplicationModule> applicationModuleList = getModel(request);
        int myCare = 0;
        int myPush = 0;
        int myWork = 0;
        int myAudit = 0;
        int waitReceive = 0;
        if (null != enterprise && null != systemUser) {
            if (checkApplication(applicationModuleList, "REPAIR")) {
                QueryParam queryParam = new QueryParam(-1, -1);
                queryParam.getSqlMap().put("AUTOMATIC_REPAIR","AUTOMATIC_REPAIR");
                queryParam.getSqlMap().put("systemUserId", systemUser.getId().toString());
                queryParam.getSqlMap().put("enterpriseId", enterprise.getId().toString());
                myCare += repairFocusBiz.totalRecord(queryParam.getSqlMap());
                queryParam.getSqlMap().clear();
                queryParam.getSqlMap().put("reportUserID", systemUser.getId().toString());
                queryParam.getSqlMap().put("AUTOMATIC_REPAIR","AUTOMATIC_REPAIR");
                queryParam.getSqlMap().put("enterpriseId", enterprise.getId().toString());
                myPush += repairOrderBiz.totalRecord(queryParam.getSqlMap());
                queryParam.getSqlMap().clear();
                queryParam.getSqlMap().put("receiveUser", systemUser.getId().toString());
                queryParam.getSqlMap().put("AUTOMATIC_REPAIR","AUTOMATIC_REPAIR");
                queryParam.getSqlMap().put("enterpriseId", enterprise.getId().toString());
                myWork += repairOrderBiz.totalRecord(queryParam.getSqlMap());
                ResultDto resultDto = repairOrderBiz.getRepairOrders("1", "10", "1", null, systemUser, enterprise, null,null,null,null,null,null,null,null,null);
                if (resultDto.getStatus() == 0) {
                    JSONObject jsonObject = (JSONObject) JSONObject.toJSON(resultDto.getData());
                    waitReceive += StringConverters.null2Int(jsonObject.getString("records"));
                }
                systemUser = systemUserBiz.findModel(systemUser.getId());
                Position position = systemUser.getPosition(enterprise, systemUser.getPositionSet());
                //查总数
                QueryParam queryParam1 = new QueryParam();
                queryParam1.getSqlMap().put("auditUserId", systemUser.getId().toString());
                queryParam1.getSqlMap().put("AUTOMATIC_REPAIR","AUTOMATIC_REPAIR");
                queryParam1.getSqlMap().put("repairOrderStatus","IN_AUDITING");
                queryParam1.getSqlMap().put("enterpriseId", enterprise.getId().toString());
                if (position != null) {
                    queryParam1.getSqlMap().put("auditPositionId", position.getId() + "");
                }
                Long records = repairOrderBiz.totalRecord(queryParam1.getSqlMap());
                myAudit += StringConverters.null2Int(records);
            }

            if (checkApplication(applicationModuleList, "TASK")) {
                QueryParam queryParam = new QueryParam(-1, -1);
                queryParam.getSqlMap().put("systemUserId", systemUser.getId().toString());
                queryParam.getSqlMap().put("enterpriseId", enterprise.getId().toString());
                myCare += taskFocusBiz.totalRecord(queryParam.getSqlMap());
                queryParam.getSqlMap().clear();
                queryParam.getSqlMap().put("createUser", systemUser.getId().toString());
                queryParam.getSqlMap().put("enterpriseId", enterprise.getId().toString());
                myPush += taskOrderBiz.totalRecord(queryParam.getSqlMap());
                queryParam.getSqlMap().clear();
                queryParam.getSqlMap().put("receiveUser", systemUser.getId().toString());
                queryParam.getSqlMap().put("enterpriseId", enterprise.getId().toString());
                myWork += taskOrderBiz.totalRecord(queryParam.getSqlMap());
                ResultDto resultDto = taskOrderBiz.getTaskOrders("1", "10", "1", null, systemUser, enterprise, null);
                if (resultDto.getStatus() == 0) {
                    JSONObject jsonObject = (JSONObject) JSONObject.toJSON(resultDto.getData());
                    waitReceive += StringConverters.null2Int(jsonObject.getString("records"));
                }
                systemUser = systemUserBiz.findModel(systemUser.getId());
                Position position = systemUser.getPosition(enterprise, systemUser.getPositionSet());
                //查总数
                QueryParam queryParam1 = new QueryParam();
                queryParam1.getSqlMap().put("auditUserId", systemUser.getId().toString());
                queryParam1.getSqlMap().put("orderStatus","IN_AUDITING");
                queryParam1.getSqlMap().put("enterpriseId", enterprise.getId().toString());
                if (position != null) {
                    queryParam1.getSqlMap().put("auditPositionId", position.getId() + "");
                }
                Long records = taskOrderBiz.totalRecord(queryParam1.getSqlMap());
                myAudit += StringConverters.null2Int(records);
            }
            if (checkApplication(applicationModuleList, "MAINTAIN")) {
                QueryParam queryParam = new QueryParam(-1, -1);
                queryParam.getSqlMap().put("systemUserId", systemUser.getId().toString());
                queryParam.getSqlMap().put("enterpriseId", enterprise.getId().toString());
                myCare += maintainFocusBiz.totalRecord(queryParam.getSqlMap());
                queryParam.getSqlMap().clear();
                queryParam.getSqlMap().put("createUserId", systemUser.getId().toString());
                queryParam.getSqlMap().put("enterpriseId", enterprise.getId().toString());
                myPush += maintainOrderBiz.totalRecord(queryParam.getSqlMap());
                queryParam.getSqlMap().clear();
                queryParam.getSqlMap().put("receiveUser", systemUser.getId().toString());
                queryParam.getSqlMap().put("enterpriseId", enterprise.getId().toString());
                myWork += maintainOrderBiz.totalRecord(queryParam.getSqlMap());
                String hql = "select obj from MaintainOrder obj where obj.deleteStatus is false and obj.enterprise.id=" + enterprise.getId();
                hql += "and (obj.orderStatusEnum = 'IN_AUDITING' or obj.delay is true or obj.cancel is true)";
                hql += " and ( obj.auditSystemUser.id = " + systemUser.getId() + " or exists( select su from SystemUser su where" +
                        " su.id=" + systemUser.getId() + " and exists( select so from su.systemUserOrganizationSet so where so.organization.id = obj.organization.id) and " +
                        " exists( select pos from su.positionSet pos where pos.id = obj.position.id)))";
                myAudit += maintainOrderBiz.hqlExcute(hql, new QueryParam(-1, -1)).size();


                ResultDto resultDto = maintainOrderBiz.list("0", "1", "10", systemUser, enterprise, null, null);
                if (resultDto.getStatus() == 0) {
                    JSONObject jsonObject = (JSONObject) JSONObject.toJSON(resultDto.getData());
                    waitReceive += StringConverters.null2Int(jsonObject.getString("records"));
                }

            }
            if (checkApplication(applicationModuleList, "INSPECTION")) {
                QueryParam queryParam = new QueryParam(-1, -1);
                queryParam.getSqlMap().put("systemUserId", systemUser.getId().toString());
                queryParam.getSqlMap().put("enterpriseId", enterprise.getId().toString());
                myCare += inspectionFocusBiz.totalRecord(queryParam.getSqlMap());
                queryParam.getSqlMap().clear();
                queryParam.getSqlMap().put("createUserId", systemUser.getId().toString());
                queryParam.getSqlMap().put("enterpriseId", enterprise.getId().toString());
                myPush += inspectionOrderBiz.totalRecord(queryParam.getSqlMap());
                queryParam.getSqlMap().clear();
                queryParam.getSqlMap().put("receiveUser", systemUser.getId().toString());
                queryParam.getSqlMap().put("enterpriseId", enterprise.getId().toString());
                myWork += inspectionOrderBiz.totalRecord(queryParam.getSqlMap());
                String hql = "select obj from InspectionOrder obj where obj.deleteStatus is false and obj.enterprise.id=" + enterprise.getId();
                hql += "and (obj.orderStatusEnum = 'IN_AUDITING' or obj.delay is true or obj.cancel is true)";
                hql += " and ( obj.auditSystemUser.id = " + systemUser.getId() + " or exists( select su from SystemUser su where" +
                        " su.id=" + systemUser.getId() + " and exists( select so from su.systemUserOrganizationSet so where so.organization.id = obj.organization.id) and " +
                        " exists( select pos from su.positionSet pos where pos.id = obj.position.id)))";
                myAudit += inspectionOrderBiz.hqlExcute(hql, new QueryParam(-1, -1)).size();

                ResultDto resultDto = inspectionOrderBiz.list("0", "1", "10", systemUser, enterprise, null, null);
                if (resultDto.getStatus() == 0) {
                    JSONObject jsonObject = (JSONObject) JSONObject.toJSON(resultDto.getData());
                    waitReceive += StringConverters.null2Int(jsonObject.getString("records"));
                }
            }
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("myCare", myCare);
        jsonObject.put("myPush", myPush);
        jsonObject.put("myWork", myWork);
        jsonObject.put("myAudit", myAudit);
        jsonObject.put("waitReceive", waitReceive);
        return ResultUtil.success(jsonObject);
    }
}
